package com.lyf.language.learn.atsgg.io.stream;

import io.netty.util.internal.AppendableCharSequence;

import java.io.*;

/**
 * @AUTHOR LYF
 * @DATE 2022/6/2
 * @VERSION 1.0
 * @DESC
 */
public class CharStreamDemo {
    File file = new File("E:\\JavaProjects\\LearnProjects\\learn-practice\\java-base\\src\\main\\java\\com\\lyf\\language\\learn\\atsgg\\io\\stream\\test1.txt");

    public static void main(String[] args) {
        CharStreamDemo charStreamDemo = new CharStreamDemo();
        //charStreamDemo.write();
        charStreamDemo.read();
    }
    /**
     * FileWriter操作
     */
    void write(){
        FileWriter fw = null;
        try {
            //fw = new FileWriter(file,true);//构造器加true,则可追加模式
            //int len;
            //fw.write("I Have a dream");

            fw = new FileWriter(file);
//            // 会覆盖-- off:str的偏移量；len：偏移的长度
//            fw.write("hello",0,2);
            //CharSequence charSequence= new AppendableCharSequence();
            ///fw.append("hello".toCharArray(),0,10);
            //依然覆盖
            fw.append('a');


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fw!=null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    /**
     * FileReader操作
     */
    void read(){
        FileReader fileReader=null;
        try {
            fileReader = new FileReader(file);
            // 固定大小，每次读取直接从0开始覆盖，若
            char[] chars =new char[1024];
            int len;
            while((len=fileReader.read(chars))!=-1){
                System.out.println(new String(chars,0,len));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileReader!=null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 文本移动、复制
     */



}
